package com.example.administrator.meishi.Dao;

import com.example.administrator.meishi.bean.MediaTitle;
import com.example.administrator.meishi.tools.OkHttpClientUtils;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/9/21.
 */
public class GridViewJson  {
    public static List<MediaTitle> getjson(String path){
        try {
             List<MediaTitle> list = new ArrayList<>();
            String json = OkHttpClientUtils.getStringFromUrl(path);
            JSONArray jsonArray = new JSONArray(json);
            for (int i = 0; i < jsonArray.length(); i++) {
                JSONObject obj = jsonArray.getJSONObject(i);
                String recommend_caption =  obj.getString("recommend_caption");
                String recommend_cover_pic = obj.getString("recommend_cover_pic");
                String type = obj.getString("type");
                if(type.equals("media")){
                    JSONObject media = obj.getJSONObject("media");
                    String caption = media.getString("caption");
                    String video = media.getString("video");
                    MediaTitle mediaTitle = new MediaTitle(caption,recommend_caption,recommend_cover_pic,video);
                    list.add(mediaTitle);
                }
            }
            return list;
        } catch (IOException e) {
            e.printStackTrace();
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return null;
    }
}
